package com.ideaction.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GlobalSwitchUtil {
    
    public static final String SIGN_SWITCH = "signSwitch";
    
    @Autowired
    private MongoTemplate funcMongoTemplate;
    
    private final static String globalSwitchCollection = "globalSwitch";
    
    private static Map<String, Boolean> globalSwitchMap = Maps.newHashMap();
    
    public void upGlobalSwitch() {
        List<JSONObject> jsonList = funcMongoTemplate.findAll(JSONObject.class, globalSwitchCollection);
        globalSwitchMap.clear();
        for (JSONObject json : jsonList) {
            for (String switchKey : json.keySet()) {
                //todo 暂时只考虑是boolean值
                if ("_id".equals(switchKey)) {
                    continue;
                }
                globalSwitchMap.put(switchKey, json.getBoolean(switchKey));
            }
        }
    }
    
    public static boolean getGlobalSwitch(String switchKey) {
        if (globalSwitchMap.containsKey(switchKey)) {
            return globalSwitchMap.get(switchKey);
        }
        
        return true;
    }
}
